<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2020-2030 衡水山木枝技术服务有限公司
 * @Date: 2021-06-10 20:18:18
 * @Description: 供应商管理
 * @LastEditors: 李志刚
 * @LastEditTime: 2021-09-17 08:27:36
 * @FilePath: /qingshanERP/app/Http/Controllers/Tenant/SupplierController.php
 */

namespace App\Http\Controllers\Tenant;

use App\Http\Controllers\Controller;
use App\Models\QingShanERP\Supplier;
use App\Models\Tenant\SupplierTenant;
use Illuminate\Http\Request;

class SupplierController extends Controller
{
    public function getList(Request $request)
    {
        try {
            $page = $request->input('page', 0);
            $size = $request->input('size', 10);
            $key = $request->input('key', '');
            $status = $request->input('status', '');
            $tenant_id = $request->tenant->tid;
            $sids = SupplierTenant::where('tenant_id',$tenant_id)->pluck('supplier_id');
            $list = Supplier::whereIn('id', $sids)->where(function ($q) use ($key) {
                if ($key != '') {
                    $q->where('suppliername', 'like', '%' . $key . '%');
                }
            })->where(function ($q) use ($status) {
                if ($status != '') {
                    $q->where('status', $status);
                }
            })->where('del_flag', 0)->offset(($page - 1) * $size)->limit($size)->orderBy('id', 'desc')->get();
            $count = Supplier::whereIn('id', $sids)->where(function ($q) use ($key) {
                if ($key != '') {
                    $q->where('suppliername', 'like', '%' . $key . '%');
                }
            })->where(function ($q) use ($status) {
                if ($status != '') {
                    $q->where('status', $status);
                }
            })->where('del_flag', 0)->count();
            $res = ['list' => $list, 'total' => $count];
            return $this->resData(200, '获取数据成功...', $res);
        } catch (\Throwable $e) {
            return $this->resData(500, '获取数据失败，请稍后再试...',$e->getLine().' - '.$e->getMessage());
        }
    }
}
